package com.koron.order.overhaul.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderSignBean;
import com.koron.order.common.mapper.OrderSignMapper;
import com.koron.order.overhaul.bean.entity.OverhaulOrderMajorFormBean;
import com.koron.order.overhaul.bean.query.OverhaulOrderMajorFormQuery;
import com.koron.order.overhaul.bean.vo.OverhaulOrderItemVO;
import com.koron.order.overhaul.bean.vo.OverhaulOrderMajorFormVO;
import com.koron.order.overhaul.mapper.OverhaulOrderMajorFormMapper;
import com.koron.order.overhaul.mapper.OverhaulOrderMapper;
import com.koron.order.overhaul.service.OverhaulOrderMajorFormService;
import com.koron.order.overhaul.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * (OverhaulOrderMajorFormBean)表服务实现类
 *
 * @author twcao
 * @since 2021-09-29 09:49:33
 */
@Service
public class OverhaulMajorFormServiceImpl implements OverhaulOrderMajorFormService {

    @Override
    @TaskAnnotation("queryDetailById")
    public OverhaulOrderMajorFormVO queryDetailById(SessionFactory factory, String formId) {
        OverhaulOrderMapper mapper = factory.getMapper(OverhaulOrderMapper.class);
        OrderSignMapper signMapper = factory.getMapper(OrderSignMapper.class);
        OrderSignBean query = new OrderSignBean();
        OverhaulOrderMajorFormVO form = mapper.getFormDetailById(formId);
        List<OverhaulOrderItemVO> itemDTOs = form.getItemDTOs();
        itemDTOs.stream().forEach(item -> {
            query.setItemId(item.getId());
            query.setOrderId(form.getOverhaulId());
            item.setSigns(signMapper.queryList(query));
        });
        form.setItemDTOs((CommonUtils.listToTree(itemDTOs, OverhaulOrderItemVO.class)));
        return form;
    }

    @Override
    public OverhaulOrderMajorFormBean queryById(SessionFactory factory, String formId) {
        OverhaulOrderMajorFormMapper mapper = factory.getMapper(OverhaulOrderMajorFormMapper.class);
        OverhaulOrderMajorFormBean formBean = mapper.queryById(formId);
        return formBean;
    }

    @Override
    public int saveOrUpdate(SessionFactory factory, List<OverhaulOrderMajorFormBean> beans) {
        Assert.notEmpty(beans, "表单集合不能为空");
        Assert.noNullElements(beans, "表单不能为空");
        OverhaulOrderMajorFormMapper mapper = factory.getMapper(OverhaulOrderMajorFormMapper.class);
        CommonUtils.update(beans);
        beans.forEach(form -> form.setId(StringUtils.isBlank(form.getId()) ? CodeTools.getCode32() : form.getId()));
        return mapper.saveOrUpdate(beans);
    }

    @Override
    public int deleteByOrderId(SessionFactory factory, String orderById) {
        OverhaulOrderMajorFormMapper mapper = factory.getMapper(OverhaulOrderMajorFormMapper.class);
        return mapper.deleteByOrderId(orderById);
    }

    @Override
    public int hiddenByOrderId(SessionFactory factory, String orderById) {
        OverhaulOrderMajorFormMapper mapper = factory.getMapper(OverhaulOrderMajorFormMapper.class);
        return mapper.hiddenByOrderId(orderById);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OverhaulOrderMajorFormBean> queryList(SessionFactory factory, OverhaulOrderMajorFormQuery query) {
        OverhaulOrderMajorFormMapper mapper = factory.getMapper(OverhaulOrderMajorFormMapper.class);
        if(query.getDeleteFlag() == null) {
            query.setDeleteFlag(0);
        }
        List<OverhaulOrderMajorFormBean> list = mapper.queryList(query);
        return list;
    }

}
